#title          : getPossibleClasses.py
#description    : outputs a list of url, various classes (tab delimited)
#author         : Arthi Ramachandran 
#date           : 20140203
#usage          : python ./getPossibleClasses.py <posts file> <number of posts per link> <number of clicks per link> <output file>
#notes          : classes are 
#                     (a) whether the link is in the top 5 percent of links by bitly clicks (binary)
#                     (b) whether the link is in the top 10 percent of links by bitly clicks (binary)
#                     (b) whether the link is in the top half percent of links by bitly clicks (binary)
#                     (b) number of bitly clicks
#                     (a) whether the link is in the top 5 percent of links by number of posts (binary)
#                     (b) whether the link is in the top 10 percent of links by number of posts (binary)
#                     (b) whether the link is in the top half percent of links by number of posts (binary)
#                     (b) number of posts
#python_version :2.7 
#============================================================================                                                                                     
import sys
import numpy as np

# for each url print the possible classes

url_info = dict()

data_filename = sys.argv[1]

def initialize_url(url):
    url_info[url] = dict()
    url_info[url]["num_posts"] = -1
    url_info[url]["num_bitly_clicks"] = -1


posted_counts_filename = sys.argv[2]
num_posts = []
with open(posted_counts_filename, "rb") as f: # number of urls posted
    for line in f:
        fields = line.strip().split(" ")
        url = fields[1]
        if url not in url_info:
            initialize_url(url)
        url_info[fields[1]]["num_posts"] = int(fields[0])
        num_posts.append(int(fields[0]))

bitly_counts_filename = sys.argv[3]
num_clicks = []
with open(bitly_counts_filename, "rb") as f: # number of bitly clicks
    for line in f:
        fields = line.strip().split(" ")
        url = fields[1]
        if url not in url_info:
            initialize_url(url)
        url_info[fields[1]]["num_bitly_clicks"] = int(fields[0])
        num_clicks.append(int(fields[0]))

top_5_posts_cutoff = np.percentile(num_posts,95)
top_10_posts_cutoff = np.percentile(num_posts,90)
top_half_posts_cutoff = np.percentile(num_posts,50)
top_5_bitly_cutoff = np.percentile(num_clicks,95)
top_10_bitly_cutoff = np.percentile(num_clicks,90)
top_half_bitly_cutoff = np.percentile(num_clicks,50)

print top_5_posts_cutoff, top_10_posts_cutoff, top_half_posts_cutoff
print top_5_bitly_cutoff, top_10_bitly_cutoff, top_half_bitly_cutoff

def getClass(count, cutoff):
    if count > cutoff:
        return "1"
    else:
        return "0"

output_filename = sys.argv[4]
fout = open(output_filename, "w+")
fout.write("URL\ttop_5_percent_bitly\ttop_10_percent_bitly\ttop_half_bitly\tbitly_clicks\ttop_5_percent_posts\ttop_10_percent_posts\ttop_half_posts\tnum_posts\n")
for url in url_info:
    top_5_posts = getClass(url_info[url]["num_posts"], top_5_posts_cutoff)
    top_10_posts = getClass(url_info[url]["num_posts"], top_10_posts_cutoff)
    top_half_posts = getClass(url_info[url]["num_posts"], top_half_posts_cutoff)
    top_5_bitly = getClass(url_info[url]["num_bitly_clicks"], top_5_bitly_cutoff)
    top_10_bitly = getClass(url_info[url]["num_bitly_clicks"], top_10_bitly_cutoff)
    top_half_bitly = getClass(url_info[url]["num_bitly_clicks"], top_half_bitly_cutoff)
    fout.write(url + "\t" + top_5_bitly + "\t" + top_10_bitly + "\t" + top_half_bitly + "\t" + str(url_info[url]["num_bitly_clicks"]) + "\t" + top_5_posts + "\t" + top_10_posts + "\t" + top_half_posts + "\t" + str(url_info[url]["num_posts"]) + "\n")

fout.close()
